package com.xysq.adapter;

/**
 * Created by Steward_Han on 2015/7/20.
 */

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rockcent.action.AppAction;
import com.rockcent.action.CallbackListener;
import com.rockcent.model.Topics;
import com.umeng.analytics.MobclickAgent;
import com.xysq.activity.LoginActivity;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;
import com.xysq.util.UseUtil;
import com.xysq.util.UserInfoKeeper;

import net.soulwolf.widget.ratiolayout.widget.RatioImageView;

public class HomepageAdapter extends BaseDataAdapter<Topics> {

    private ViewHolder viewHolder;
    private AppAction appAction;

    public HomepageAdapter(Context context, AppAction appAction) {
        super(context);
        this.appAction = appAction;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        viewHolder = new ViewHolder();
        if (null == convertView) {
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.item_homepage, null);
            int screenWidth = UseUtil.getScreenWidth((Activity) context);
            viewHolder.picImg = (RatioImageView) convertView.findViewById(R.id.img_pic);
            viewHolder.picImg.setMinimumHeight(screenWidth / 5 * 3);
            viewHolder.likeCountTxt = (TextView) convertView.findViewById(R.id.txt_like_count);
            viewHolder.heartImg = (ImageView) convertView.findViewById(R.id.img_heart);
            viewHolder.behaviorLayout = (LinearLayout) convertView.findViewById(R.id.layout_behavior);
            viewHolder.titleTxt = (TextView) convertView.findViewById(R.id.txt_title);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final Topics topics = itemList.get(position);
        if (null != topics) {
            viewHolder.titleTxt.setText(topics.getName());
            final String picUrl[] = split(topics.getPicUrl());
            imageLoader.displayImage(PropertiesUtil.getImageServer() + picUrl[0], viewHolder.picImg);
//            viewHolder.picImg.setTag(topics.getPicUrl());
//            imageLoader.loadImage(PropertiesUtil.getImageServer() + picUrl[0], new ImageLoadingListener() {
//                @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
//                @Override
//                public void onLoadingStarted(String imageUri, View view) {
////                    if(viewHolder.picImg.getTag().toString().equals(topics.getPicUrl())) {
////                        viewHolder.picImg.setBackground(context.getResources().getDrawable(R.drawable.def_img));
////                    }
//                }
//
//                @Override
//                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
//                }
//
//                @Override
//                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//                    if(viewHolder.picImg.getTag().toString().equals(topics.getPicUrl())) {
//                        viewHolder.picImg.setImageBitmap(loadedImage);
//                        notifyDataSetChanged();
//                    }
//                }
//
//                @Override
//                public void onLoadingCancelled(String imageUri, View view) {
//                }
//            });
            if ((UserInfoKeeper.readIsLike(context, topics.getUuid()).equals("false"))) {
                viewHolder.heartImg.setBackgroundResource(R.drawable.ic_love_hollow_small);
            } else if (UserInfoKeeper.readIsLike(context, topics.getUuid()).equals("true")) {
                viewHolder.heartImg.setBackgroundResource(R.drawable.ic_love_solid_small);
            } else {
                ToastUtil.showLong(context, "获取用户是否已经喜欢失败");
            }
            viewHolder.likeCountTxt.setText(String.valueOf(topics.getLikeCount()));
            viewHolder.behaviorLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view) {
                    MobclickAgent.onEvent(context, "1005");
                    if ((UserInfoKeeper.readIsLike(context, topics.getUuid()).equals("false"))) {
                        if (TextUtils.isEmpty(UserInfoKeeper.readPhoneNumber(context))) {
                            context.startActivity(new Intent(context, LoginActivity.class));
                        } else {
                            appAction.behavior(topics.getUuid(), UserInfoKeeper.readCustomerId(context), "2", new CallbackListener<Void>() {
                                @Override
                                public void onSuccess(Void data) {
                                    UserInfoKeeper.writeIsLike(context, "true", topics.getUuid());
                                    viewHolder.heartImg.setBackgroundResource(R.drawable.ic_love_solid_small);
                                    topics.setLikeCount(topics.getLikeCount() + 1);
                                    notifyDataSetChanged();
                                }

                                @Override
                                public void onFailure(String errorCode, String message) {
                                    ToastUtil.showShort(context, "不好意思您的love操作失败了");
                                }
                            });
                        }
                    } else if (UserInfoKeeper.readIsLike(context, topics.getUuid()).equals("true")) {
                        ToastUtil.showLong(context, "您已经love了该专题");
                    }
                }
            });
        }
        return convertView;
    }

    private static class ViewHolder {
        RatioImageView picImg;
        TextView likeCountTxt;
        ImageView heartImg;
        LinearLayout behaviorLayout;
        TextView titleTxt;

    }

    private String[] split(String picUrls) {
        String[] picUrl = picUrls.split(";");
        return picUrl;
    }
}
